package AspectoLog;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.aspectj.lang.Signature;


public aspect AspectoLogGetsESets {
	FileWriter f = null;
	
	pointcut logarGets(): get( private * *);
		
	//	execution (* *.get*(..));	
	
	before(): logarGets() {	
		String logGet = new String("");
		Calendar tempo = new GregorianCalendar();
		String tm = tempo.getTime().toString();
		
		Signature sig = thisJoinPointStaticPart.getSignature();
		String classe = sig.getDeclaringType().getName();
		String metodo = sig.getName();
		
		logGet = ("*LOG: Atributo Acessado: " + classe + "." + metodo
				 	+ ": tempo -> " + tm  );
		//System.out.println(logGet);
		
//		try {
//			f = new FileWriter("LogGets.txt");
//			f.write(" nova chamada do metodo get!! " +"\n" );
//			f.write( thisJoinPointStaticPart.getSignature().getDeclaringTypeName() );
//			f.write(logGet);
//			f.close();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		
		// testando escrita em arquivo
		ArqLog log = new ArqLog("LogGets.log");
		log.iniciar();
		log.arquivar(thisJoinPoint);
		
		
		
//		logGet = ("*Atributo Acessado: " + thisJoinPoint.toLongString() 
//				 + ": tempo -> " + new Date().getTime()  );
//		System.out.println(logGet); // comentar depois de testar
//		
//		FileWriter f = null;
//		try {
//			f = new FileWriter("LogGetsESets.txt");
//			f.write(" LogGetsESets!!  \n");
//			f.write( thisJoinPointStaticPart.getSignature().getName() );
//		} catch (IOException e) {
//			e.printStackTrace();
//		}		
		//System.out.println(" Fim LogGetsESets !" + thisJoinPoint );
		
		
		//System.out.println(" log Get !" + thisJoinPoint );
		
	}
		
	pointcut logarSets(): set(private * *);
//		execution (* *.set*(..));	
	before(): logarSets() {
		String logSet = new String("");
		Calendar tempo = new GregorianCalendar();
		String tm = tempo.getTime().toString();
		
		Signature sig = thisJoinPointStaticPart.getSignature();
		String classe = sig.getDeclaringType().getName();
		String metodo = sig.getName();
		
		logSet = ("*LOG: Atributo Definido: " + classe + "." + metodo
				 	+ ": tempo -> " + tm  );
		//System.out.println(logSet);
//		try {
//			f = new FileWriter("LogSets.txt");
//			f.write(" nova chamada do metodo set!! " +"\n" );
//			f.write( thisJoinPointStaticPart.getSignature().getDeclaringTypeName() );
//			f.write(logSet );
//			f.close();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		
		
		// testando escrita em arquivo?
		ArqLog log = new ArqLog("LogSets.log");
		log.iniciar();
		log.arquivar(thisJoinPoint  );
		
	}
	
	
	pointcut logarToString(): 
		execution (* *.toString*(..));
	after(): logarToString() {
		String logString = new String("");	
		Calendar tempo = new GregorianCalendar();
		String tm = tempo.getTime().toString();
		
		Signature sig = thisJoinPointStaticPart.getSignature();
		String classe = sig.getDeclaringType().getName();
		String metodo = sig.getName();		
		logString = ("*LOG: toString: " + classe + "." + metodo
				 	+ ": tempo -> " + tm  );
	//	System.out.println(logString);
//		try {
//			f = new FileWriter("LogToString.txt");
//			f.write(" nova chamada do metodo toString!! " +"\n" );
//			f.write( thisJoinPointStaticPart.getSignature().getDeclaringTypeName() );
//			f.write(logString);
//			f.close();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		
		
		// testando escrita em arquivo
		ArqLog log = new ArqLog("LogToString.log");
		log.iniciar();
		log.arquivar(thisJoinPoint);
		
//		String logToString = new String("");
//		logToString = ("*Antes do toString : " + thisJoinPoint.toLongString()
//				 + ": tempo -> " + new Date().getTime()  );
//		System.out.println(logToString); // comentar depois de testar
		
		//System.out.println(" log toString !"  + thisJoinPoint);
	}
	
	
	
		
}
